//
//  SPPlayer.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import "SPPlayer.h"
#import "SPCard.h"

@interface SPPlayer ()

@property (nonatomic, strong, readwrite) NSString *playerId;
@property (nonatomic, assign, readwrite) BOOL isAI;

@end

@implementation SPPlayer

#pragma mark - Initialization

- (instancetype)initWithName:(NSString *)name chips:(NSInteger)chips {
    if (self = [super init]) {
        _playerId = [[NSUUID UUID] UUIDString];
        _name = name;
        _chips = chips;
        _isAI = NO;
        _holeCards = [[NSMutableArray alloc] init];
        [self resetForNewGame];
    }
    return self;
}

- (instancetype)initWithName:(NSString *)name 
                       chips:(NSInteger)chips 
                aiDifficulty:(SPAIDifficulty)difficulty {
    if (self = [super init]) {
        _playerId = [[NSUUID UUID] UUIDString];
        _name = name;
        _chips = chips;
        _isAI = YES;
        _aiDifficulty = difficulty;
        _holeCards = [[NSMutableArray alloc] init];
        [self resetForNewGame];
    }
    return self;
}

#pragma mark - Card Management

- (void)addHoleCard:(SPCard *)card {
    if (self.holeCards.count < SP_CARDS_PER_PLAYER) {
        [self.holeCards addObject:card];
    }
}

- (void)clearHoleCards {
    [self.holeCards removeAllObjects];
}

#pragma mark - Betting Actions

- (BOOL)bet:(NSInteger)amount {
    if (![self canBet:amount]) {
        return NO;
    }
    
    self.chips -= amount;
    self.currentBet += amount;
    self.totalBet += amount;
    
    if (self.chips == 0) {
        self.isAllIn = YES;
    }
    
    return YES;
}

- (BOOL)call:(NSInteger)amount {
    return [self bet:amount];
}

- (BOOL)raise:(NSInteger)amount {
    return [self bet:amount];
}

- (NSInteger)allIn {
    NSInteger allInAmount = self.chips;
    if (allInAmount > 0) {
        [self bet:allInAmount];
    }
    return allInAmount;
}

- (void)fold {
    self.hasFolded = YES;
}

#pragma mark - State Management

- (void)resetForNewRound {
    self.currentBet = 0;
    self.hasFolded = NO;
    self.isDealer = NO;
    self.isSmallBlind = NO;
    self.isBigBlind = NO;
    [self clearHoleCards];
}

- (void)resetForNewGame {
    [self resetForNewRound];
    self.totalBet = 0;
    self.isAllIn = NO;
    self.position = -1;
}

#pragma mark - Utility Methods

- (NSInteger)availableChips {
    return self.chips;
}

- (BOOL)canBet:(NSInteger)amount {
    return (amount > 0 && amount <= self.chips && !self.hasFolded && !self.isAllIn);
}

#pragma mark - NSObject

- (BOOL)isEqual:(id)object {
    if (![object isKindOfClass:[SPPlayer class]]) {
        return NO;
    }
    
    SPPlayer *otherPlayer = (SPPlayer *)object;
    return [self.playerId isEqualToString:otherPlayer.playerId];
}

- (NSUInteger)hash {
    return [self.playerId hash];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Player: %@ (Chips: %ld, AI: %@)", 
            self.name, (long)self.chips, self.isAI ? @"YES" : @"NO"];
}

@end